package com.cloud.system.api.mapper;

import java.util.List;

import com.cloud.system.api.bean.NoticeBean;
import com.cloud.system.api.bean.UnitCriteriaBean;
import com.cloud.system.api.provider.UnitPageProvider;
import org.apache.ibatis.annotations.*;

import com.cloud.system.api.entity.UnitInfo;
import com.cloud.system.common.driver.SimpleInlangDriver;
import com.cloud.system.common.result.SelectItem;
import com.cloud.system.common.utils.BaseMapperHelper;

public interface UnitInfoMapper extends BaseMapperHelper<UnitInfo> {

    @Select("select senior_id from unit_info where delete_flag = 0 and senior_id in (#{unitIds}) and unit_type_id in (#{unitTypeIDs}) group by senior_id having count(1) > 0")
    @Lang(SimpleInlangDriver.class)
    @ResultType(value = Long.class)
    List<Long> selectUnitChildrenCount(@Param("unitIds") List<Long> ids, @Param("unitTypeIDs") List<Integer> unitTypeIDs);


    @Select("select senior_id from unit_info where delete_flag = 0 and senior_id in (#{unitIds}) and unit_type_id in (#{unitTypeIDs}) and unit_id in(select unit_id from unit_domain where sub_domain_type_id in (#{doMainList})) group by senior_id having count(1) > 0")
    @Lang(SimpleInlangDriver.class)
    @ResultType(value = Long.class)
    List<Long> selectUnitChildrenCountByDomain(@Param("unitIds") List<Long> ids, @Param("doMainList") List<String> doMainList, @Param("unitTypeIDs") List<Integer> unitTypeIDs);

    @Select("select unit_name as label, unit_id as value from unit_info where unit_id in (#{unitIds}) and delete_flag= 0 and unit_type_id = '1'")
    @Lang(SimpleInlangDriver.class)
    @Results({@Result(property = "label", column = "label", javaType = String.class),
            @Result(property = "value", column = "value", javaType = Object.class),})
    List<SelectItem> listSeniorSubject(@Param("unitIds") String[] unitIds);

    @SelectProvider(type = UnitPageProvider.class, method = "listPage")
    @Results({@Result(property = "unitId", column = "unit_id", javaType = Long.class),
            @Result(property = "unitName", column = "unit_name", javaType = String.class),
            @Result(property = "unitAddress", column = "unit_address", javaType = String.class),
            @Result(property = "socialCreditCode", column = "social_credit_code", javaType = String.class),
            @Result(property = "regionName", column = "region_name", javaType = String.class),
            @Result(property = "unitTypeId", column = "unit_type_id", javaType = String.class),
            @Result(property = "unitLevel", column = "unit_level", javaType = String.class),
            @Result(property = "unitNatureId", column = "unit_nature_id", javaType = String.class),
            @Result(property = "regionCode", column = "region_code", javaType = String.class),
    })
    List<UnitInfo> listPage(UnitCriteriaBean criteria);

    @Select("select count(*) from unit_info where unit_name = #{unitName} and delete_flag != 1 and ( senior_id = #{seniorId} or unit_id = #{seniorId})")
    @ResultType(Integer.class)
    int selectByUnitName(@Param("unitName") String unitName, @Param("seniorId")Long unitId);


    @Select("select count(*) from unit_info where unit_type_id = '0' and region_code = #{regionCode} and unit_id != #{unitId} and delete_flag != 1")
    @ResultType(Integer.class)
    int selectRegionCodeByCode(@Param("regionCode") String regionCode,@Param("unitId")Long unitId);

    @SelectProvider(type = UnitPageProvider.class, method = "unitInfoListPage")
    @Results({@Result(property = "unitId", column = "unit_id", javaType = Long.class),
            @Result(property = "unitName", column = "unit_name", javaType = String.class)})
    List<UnitInfo> listUnitInfoPage(UnitCriteriaBean criteria);

    @Insert("INSERT INTO  cda . unit_info  (  unit_name ,  short_name ,  unit_nature_id ,  unit_level ,  region_code ,  manubrium_flag ,  unit_type_id ,  subject_type ,  senior_id ,  unit_code ,  display_order ,  delete_flag ,  region_name , sup_unit_level ,  import_batch , is_law_organs ) VALUES " +
            " (#{unitName},  #{shortName} ,  #{unitNatureId} ,  #{unitLevel} ,  #{regionCode} ,  #{manubriumFlag} ,  #{unitTypeId} ,  #{subjectType} ,  #{seniorId} ,  #{unitCode} ,  #{displayOrder} ,  #{deleteFlag} ,  #{regionName} , #{supUnitLevel} ,  #{importBatch} , #{isLawOrgans}) ")
    @Options(useGeneratedKeys = true, keyProperty = "unitId")
    Integer insertUnitInfo(UnitInfo unitInfo);

    @Select("select unit_code from unit_info where unit_id = (select senior_id from unit_info where unit_id = #{unitId})")
    @ResultType(String.class)
    String selectParentUnitCodeByUnitId(Long unitId);
}
